package com.apes.scm.settle.settle.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.scm.account.account.repository.CooperationRepository;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.settle.settle.model.SettleReceivable;
import com.apes.scm.settle.settle.respository.SettleReceivableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*
 * 采购结算
 * */
@Service("settleReceivableService")
public class SettleReceivableService extends DomainService {

    @Autowired
    private SettleReceivableRepository settleReceivableRepository;

    @Autowired
    private SettleReceivableDao settleReceivableDao;

    @Autowired
    private PartyRoleRepository partyRoleRepository;

    /**
     * 生成报表
     *
     * @param request
     */
    private void generate(SimpleRequest request) throws ParseException {
        //获取日期
        Date taskDate = new SimpleDateFormat("yyyy-MM").parse(request.get("startDate").toString());
        Date date = DateUtil.dateToDate(StringUtils.isEmpty(taskDate) ? new Date() : taskDate, DateUtil.SHOW_DATE_FORMAT);
        List<SettleReceivable> yearMonth = settleReceivableRepository.findAll(JpaDsl.toCriteriaByEq("yearMonth", taskDate));
        settleReceivableRepository.deleteAll(yearMonth);
        List<SettleReceivable> settleReceivables = new ArrayList<>();
        settleReceivableDao.queryIncome().forEach(result -> {
            settleReceivables.add(buildIncomeSummary(result, date));
        });
        settleReceivableRepository.saveAll(settleReceivables);
    }


    /**
     * 批量生成
     *
     * @param request
     */
    public void batchGenerate(SimpleRequest request) throws ParseException {
        Date startDate = DateUtil.dateToDate(new SimpleDateFormat("yyyy-MM").parse(request.get("startDate").toString()), DateUtil.SHOW_MONTH_FORMAT);
        Date endDate = DateUtil.dateToDate(new SimpleDateFormat("yyyy-MM").parse(request.get("endDate").toString()), DateUtil.SHOW_MONTH_FORMAT);
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);
        calendar.add(Calendar.MONTH, 1);
        //获取营业收入枚举
        do {
            List<SettleReceivable> yearMonth = settleReceivableRepository.findAll(JpaDsl.toCriteriaByEq("yearMonth", c.getTime()));
            settleReceivableRepository.deleteAll(yearMonth);
            List<SettleReceivable> settleReceivables = new ArrayList<>();
            settleReceivableDao.queryIncome().forEach(result -> {
                settleReceivables.add(buildIncomeSummary(result, c.getTime()));
            });
            c.add(Calendar.MONTH, 1);
            settleReceivableRepository.saveAll(settleReceivables);
        } while (!c.getTime().equals(calendar.getTime()));
    }


    private SettleReceivable buildIncomeSummary(Map result, Date date) {
        SettleReceivable settleReceivable = new SettleReceivable();
        String cooperation = "IM_OWN".equals(result.get("INVENTORY_MODE").toString()) ? "distribution" : "consignment";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        List<Map> maps = settleReceivableDao.queryCompanyPartner(result.get("COMPANY_ID").toString(), result.get("PARTNER_ID").toString(),
                result.get("INVENTORY_MODE").toString(), c.getTime());
        settleReceivable.setCooperation(SpringManager.getBean(CooperationRepository.class).findOne(cooperation));
        settleReceivable.setCompany(partyRoleRepository.findOne(result.get("COMPANY_ID").toString()));
        settleReceivable.setPartner(partyRoleRepository.findOne(result.get("PARTNER_ID").toString()));
        settleReceivable.setYearMonth(date);
        settleReceivable.setAmount(maps.size() < 1 ? 0 : Double.valueOf(maps.get(0).get("AMOUNT").toString()));
        settleReceivable.setCreateDate(new Date());
        return settleReceivable;
    }

}

